package com.qf.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.bean.JsonResult;
import com.qf.pojo.GoodsCategory;
import com.qf.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("category")
public class CategoryController {

    @Autowired
    private CategoryService service;


    //查
    @RequestMapping("findByCategoryPage")
    @ResponseBody
    public JsonResult findByCategoryPage(
            @RequestParam(value = "page", required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(value = "limit", required = false, defaultValue = "5") Integer pageSize)
    {
       //设置分页参数
        PageHelper.startPage(pageNum,pageSize);
        //查询所有
        List<GoodsCategory> categoryAll = service.findAll();
        //封装到PageInfo对象
        PageInfo<GoodsCategory> pageInfo = new PageInfo<>(categoryAll);
        //返回JsonResult对象
        JsonResult jsonResult = JsonResult.ok();
        jsonResult.setCount(pageInfo.getTotal());
        jsonResult.setData(pageInfo.getList());

        return jsonResult;
    }

    //增
    @RequestMapping("addCategory")
    public String addCategory(GoodsCategory goodsCategory){

        System.out.println(goodsCategory);
        //调用方法
        service.addCategory(goodsCategory);
        return "redirect:/index.html";
    }

    //删
    @RequestMapping("deleteCategoryById")
    @ResponseBody
    public String deleteCategoryById(Integer id){

        System.out.println(id);
        service.deleteCategoryById(id);
        return "success";
    }



    //改

    @RequestMapping("updateCategory")
    public String updateCategory(GoodsCategory goodsCategory){

        System.out.println(goodsCategory);
        service.updateCategory(goodsCategory);
        return "redirect:/index.html";
    }


}
